package com.example.interceptor.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.interceptor.entity.User;
import com.example.interceptor.service.UserService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class IntdexController {

        @Autowired
        UserService userService;
        @GetMapping("/login")
        public String login(){
            return "comm/login";//classpath:/templates/comm/login.html
        }
        @GetMapping("/index")
        public String index(){
            return "client/index";
        }
        @GetMapping("/backIndex")
        public String backIndex(){
            return "back/index";
        }

        @PostMapping("/login")
        @Test
        public String toLogin(String username, String password, Model model, HttpSession session){
//            QueryWrapper<User> wrapper = (QueryWrapper<User>) userService.getAllUser();
//            wrapper.eq("username", username).eq("password",password);
            List<User> userList = userService.getAllUser();

            if(username ==null || username.isEmpty() || password==null || password.isEmpty()){
                model.addAttribute("error",true);
                return "comm/login";
            }else  if (
                    userList!=null
//                    username.equals("张三") && password.equals("123456")

            ){
                session.setAttribute("username","张三");
                return "client/index";
            }else{
                model.addAttribute("error",true);
                return "comm/login";
            }


        }

}
